package com.yufei.utils;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.yufei.annotation.ExcelFieldOrder;
import com.yufei.annotation.ExcelHeaders;
import com.yufei.annotation.QueryEnable;


/**
 * @author jasstion
 * 2013-2-9
 * 注解操作相关的工具类
 */
public class AnnotationUtil {

	public static List<String> getQueryEnableFieldNames(Class entityClass){
		List<String> fieldNames=new ArrayList<String>();
		List<Field> fields=CommonUtil.getAllDeclareFields(entityClass);
		for(Field field :fields){
			if(AnnotationUtil.fieldIsQueryEnable(field)){
				fieldNames.add(field.getName());
			}
		}
		return fieldNames;
	}

	public static Boolean fieldIsQueryEnable(Field field){
		QueryEnable   queryEnable= (QueryEnable) field.getAnnotation(QueryEnable.class);
		if(queryEnable==null){
	      return false;
	    }
	    return queryEnable.enable();
	}
	/**
	 * @param entityClass
	 * @return
	 * 
	 */
	public static List<String> getFieldOrders(Class entityClass){
		List<String> fieldOrders=new ArrayList<String>();
		ExcelFieldOrder excelFAnnotation=(ExcelFieldOrder) entityClass.getAnnotation(ExcelFieldOrder.class);
		if(excelFAnnotation!=null){
			for(String fieldOrder:excelFAnnotation.fieldOrders().split(",")){
				fieldOrders.add(fieldOrder);
			}
			}
		
		return fieldOrders;
	}
	public static List<String> getExcelHeaders(Class entityClass){
		List<String> fieldOrders=new ArrayList<String>();
		ExcelHeaders excelFAnnotation=(ExcelHeaders) entityClass.getAnnotation(ExcelHeaders.class);
		if(excelFAnnotation!=null){
			for(String fieldOrder:excelFAnnotation.headers().split(",")){
				fieldOrders.add(fieldOrder);
			}
		}
		
		return fieldOrders;
	}

}
